package tmproject.hlhj.fhp.cartrans.activitys

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.huantansheng.easyphotos.EasyPhotos
import kotlinx.android.synthetic.main.aty_exam_detail.*
import org.greenrobot.eventbus.EventBus
import tmproject.hlhj.fhp.cartrans.BaseAty
import tmproject.hlhj.fhp.cartrans.IView.ExamDetailView
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.adapter.ExamChooseAdp
import tmproject.hlhj.fhp.cartrans.customVIews.ConfirmDialog
import tmproject.hlhj.fhp.cartrans.event.PathEvent
import tmproject.hlhj.fhp.cartrans.javaBean.ExamDetailBean
import tmproject.hlhj.fhp.cartrans.log
import tmproject.hlhj.fhp.cartrans.presenter.ExamDetailPresenter
import tmproject.hlhj.fhp.cartrans.utils.SpHelper
import tmproject.hlhj.fhp.cartrans.utils.ToastUtils

class ExamDetailAty:BaseAty<ExamDetailView,ExamDetailPresenter>(), ExamDetailView {
    private lateinit var chooseAdp:ExamChooseAdp
    private val chooseDatas=ArrayList<String>()
    private val chooseIndex=ArrayList<Int>()
    private var nowIndex=0
    private val TAKE_PHOTO=111

    private val checkHash=HashMap<Int,Boolean>()
    companion object {
        var isShowResult:Int=0
    }
    private var rightNum=0
    private val testDatas=ArrayList<ExamDetailBean.DataBean>()
    private val tempRightAnwser=ArrayList<String>()
    override fun setExamDetail(examDetailBean: ExamDetailBean) {
        testDatas.addAll(examDetailBean.data)
        val spBean = SpHelper.getExam(this, intent.getIntExtra("id", -1))
        if (spBean!=null){
            rightNum=spBean.rightNum
            nowIndex=spBean.nowIndex
        }
        setData()
    }

    /**
     * 数据使化
     */
    private fun setData() {
        isShowResult=0
        testText.text = testDatas[nowIndex].stem
        testProgress.text="${nowIndex+1}/${testDatas.size}"
        chooseDatas.addAll(testDatas[nowIndex].options)
        chooseDatas.forEachIndexed { index, s ->
            checkHash.put(index,false)
        }
        tempRightAnwser.clear()
        tempRightAnwser.addAll(testDatas[nowIndex].correct_options.split(","))
        chooseAdp.notifyDataSetChanged()
        if (testDatas[nowIndex].type.equals("SC")) {
            testType.text = "单选题"
        } else if (testDatas[nowIndex].type.equals("MC")) {
            testType.text = "多选题"
        } else if (testDatas[nowIndex].type.equals("TF")) {
            testType.text = "判断题"
        }
    }
    override fun initView() {
        chooseAdp= ExamChooseAdp(chooseDatas,checkHash,0,tempRightAnwser)
        recyclerview.adapter=chooseAdp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)

    }

    override fun logicStart() {
        presenter?.getExamDetail(this,SpHelper.getToken(this),intent.getIntExtra("id",-1))
    }

    override fun setListener() {
        chooseAdp.setOnItemClickListener { adapter, view, position ->
            when(testDatas[nowIndex].type){
                "SC"->{
                    if (chooseIndex.size==0){
                        chooseIndex.add(position)
                    }else{
                        chooseIndex[0]=position
                    }
                    for (i in 0 until checkHash.size){
                        if (i==position){
                            checkHash.put(i,true)
                        }else{
                            checkHash.put(i,false)
                        }
                    }
                    chooseAdp.notifyDataSetChanged()
                    /*checkHash.forEach { i, b ->
                        if (i==position){
                            checkHash.put(i,true)
                        }else{
                            checkHash.put(i,false)
                        }
                    }*/
                }
                "MC"->{
                    if (checkHash[position]!!){
                        checkHash.put(position,false)
                        chooseIndex.remove(position)
                    }else{
                        checkHash.put(position,true)
                        chooseIndex.add(position)
                    }

                    chooseAdp.notifyDataSetChanged()
                }
                "TF"->{
                    if (chooseIndex.size==0){
                        chooseIndex.add(position)
                    }else{
                        chooseIndex[0]=position
                    }

                    for (i in 0 until checkHash.size){
                        if (i==position){
                            checkHash.put(i,true)
                        }else{
                            checkHash.put(i,false)
                        }
                    }
                    chooseAdp.notifyDataSetChanged()
                }
            }
        }
        btNext.setOnClickListener {
            if (nowIndex>=testDatas.size-1){
                nowIndex++
                isShowResult=1
                chooseAdp.notifyDataSetChanged()
                over()
                return@setOnClickListener
            }
            when(testDatas[nowIndex].type){
                "SC"->{
                    if (chooseIndex[0].toString().equals(testDatas[nowIndex].correct_options)){
                        chooseRight()
                    }else{
                        chooseWrong()
                    }
                }
                "MC"->{
                    var isRight=true
                    val rightAnwsers = testDatas[nowIndex].correct_options.split(",")
                    if (chooseIndex.size!=rightAnwsers.size){
                        isRight=false
                        chooseWrong()
                        return@setOnClickListener
                    }
                    when(chooseIndex.size){
                        1->{

                        }
                        2->{
                            if(chooseIndex[0]>chooseIndex[1]){
                                val temp = chooseIndex[0]
                                chooseIndex[0]=chooseIndex[1]
                                chooseIndex[1]=temp
                            }
                        }
                        3->{
                            for (i in 0..chooseIndex.size-2){
                                if (chooseIndex[i]>chooseIndex[i+1]){
                                    val temp = chooseIndex[i]
                                    chooseIndex[i]=chooseIndex[i+1]
                                    chooseIndex[i+1]=temp
                                }
                            }
                        }
                        else->{
                            for (i in 0..chooseIndex.size-2){
                                if (chooseIndex[i]>chooseIndex[i+1]){
                                    val temp = chooseIndex[i]
                                    chooseIndex[i]=chooseIndex[i+1]
                                    chooseIndex[i+1]=temp
                                }
                            }
                        }
                    }
                    log("fhp","选了的选项${chooseIndex.toString()}")

                    for (i in 0..chooseIndex.size-1){
                        if (!chooseIndex[i].toString().equals(rightAnwsers[i])){
                            isRight=false
                        }
                    }
                    if (isRight){
                        chooseRight()
                    }else{
                        chooseWrong()
                    }
                }
                "TF"->{
                    if (chooseIndex[0].toString().equals(testDatas[nowIndex].correct_options)){
                        chooseRight()
                    }else{
                        chooseWrong()
                    }
                }
            }
        }
    }
    private fun over(){
        recyclerview.postDelayed(Runnable {
            ToastUtils.toast("试卷已填写完毕，请提交认证")
            val bundle = Bundle()
            bundle.putInt("id",intent.getIntExtra("id",-1))
            bundle.putInt("cNum",rightNum)
            SpHelper.saveExam(this,nowIndex,rightNum,intent.getIntExtra("id",-1))//保存进度
            val confirmDialog = ConfirmDialog(this, 1, bundle)
            confirmDialog.setOnDismissListener {
                finish()
            }
            /*confirmDialog.listener=object :ConfirmDialog.OnCommitListener{
                override fun over() {
                    finish()
                }
            }*/
            confirmDialog.show()
        },1000)

//        ToastUtils.toast("答完了")
    }
    private fun chooseWrong(){

        goNext()

    }
    private fun chooseRight(){

        rightNum++
        goNext()
    }
    private fun goNext(){
        isShowResult=1
        chooseAdp.notifyDataSetChanged()

        recyclerview.postDelayed({
            if (nowIndex>=testDatas.size-1){
                /*nowIndex++
                over()*/
            }else{
                nowIndex++
                chooseIndex.clear()
                chooseDatas.clear()
                SpHelper.saveExam(this,nowIndex,rightNum,intent.getIntExtra("id",-1))//保存进度

                setData()
            }
        },2000)
    }

    override fun getContentId(): Int = R.layout.aty_exam_detail

    override fun bindView(): ExamDetailView {
        return this
    }

    override fun createPresenter(): ExamDetailPresenter {
        return ExamDetailPresenter()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        when(requestCode){
            TAKE_PHOTO->{
                val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
                val pathEvent = PathEvent()
                pathEvent.path=pathList[0]
                EventBus.getDefault().post(pathEvent)
            }
        }
    }
}